﻿package frEngine.animateControler
{
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.animateControler.keyframe.*;
    import frEngine.effectEditTool.parser.def.*;
    import frEngine.effectEditTool.temple.*;
    import frEngine.event.*;
    import frEngine.math.*;

    public class FightObjectControler extends Modifier
    {
        private var _tempVect:Vector3D;
        private var _const1:Number = 57.2958;
        private var _endPos:Vector3D;
        private var _startPos:Vector3D;
        private var _midPos:Vector3D;
        private var isLinear:Boolean;
        public var playDringFrame:int;
        private var curPos:Vector3D;
        private var _keepGoing:Boolean = false;
        private var _followTarget:Boolean = true;
        private var _staticFaceTarget:Boolean = false;
        private var _templeFight:TempleFight;
        private var _fightParams:FightParams;
        private var _onhitCallBack:Function;
        private var _curTargetObj:Pivot3D;
        private var _curOffsetFrame:int = 0;
        private var _preCol3:Vector3D;

        public function FightObjectControler()
        {
            this._tempVect = new Vector3D();
            this._endPos = new Vector3D();
            this._startPos = new Vector3D();
            this.curPos = new Vector3D();
            this._preCol3 = new Vector3D();
            return;
        }// end function

        override public function get type() : int
        {
            return AnimateControlerType.FightObject;
        }// end function

        private function nextStep() : void
        {
            if (this._fightParams.next)
            {
                this._fightParams = this._fightParams.next;
                this.setFightParams();
                this._curOffsetFrame = this.currentFrame;
            }
            else
            {
                this.callBack(false);
            }
            return;
        }// end function

        private function callBack(param1:Boolean) : void
        {
            if (this._onhitCallBack != null)
            {
                this._onhitCallBack.apply(null, [param1]);
                this._onhitCallBack = null;
            }
            return;
        }// end function

        public function resetEmmiter(param1:TempleFight, param2:Function, param3:FightParams) : void
        {
            this._templeFight = param1;
            this._fightParams = param3;
            this._onhitCallBack = param2;
            this._curOffsetFrame = 0;
            if (this._templeFight)
            {
                this.setFightParams();
            }
            if (targetObject3d)
            {
                targetObject3d.setPosition(this.curPos.x, this.curPos.y, this.curPos.z, false);
                targetObject3d.setRotation(0, 0, 0);
            }
            return;
        }// end function

        private function setFightParams() : void
        {
            if (!this._templeFight)
            {
                return;
            }
            this._startPos = this._templeFight.getPos(this._fightParams.startTargetLabel, this._fightParams.startBoneName, true, true);
            this._endPos = this._templeFight.getPos(this._fightParams.endTargetLabel, this._fightParams.endBoneName, false, true);
            if (!this._endPos || !this._startPos)
            {
                this._templeFight = null;
                this.callBack(true);
                return;
            }
            this._fightParams.startPos = this._startPos;
            this._fightParams.endPos = this._endPos;
            this.isLinear = this._fightParams.lineType == ELineType.linear;
            var _loc_2:* = this._fightParams.curveMidPos;
            this._midPos = this._fightParams.curveMidPos;
            var _loc_1:* = this._endPos.subtract(this._startPos).length;
            this.playDringFrame = _loc_1 / this._fightParams.fightSpeed;
            var _loc_2:int = 1;
            this.playDringFrame = 1;
            this._keepGoing = this._fightParams.keepGoing;
            this._followTarget = this._fightParams.followTarget;
            this._staticFaceTarget = this._fightParams.staticFaceTarget;
            this.curPos = this._startPos.clone();
            this._curTargetObj = this._templeFight.getObj3dByFlag(this._fightParams.endTargetLabel);
            return;
        }// end function

        override public function toUpdateAnimate(param1:Boolean = false) : void
        {
            var _loc_2:Boolean = false;
            if (!this._templeFight || !this._templeFight.targetRole || !this._fightParams)
            {
                return;
            }
            this.currentFrame = targetObject3d.timerContorler.curFrame;
            if (!param1 && !changeFrame)
            {
                return;
            }
            if (this._staticFaceTarget)
            {
                _loc_2 = this.staticFaceTargetHander();
            }
            else
            {
                _loc_2 = this.linearMoving();
            }
            if (_loc_2)
            {
                this.nextStep();
            }
            return;
        }// end function

        private function staticFaceTargetHander() : Boolean
        {
            var _loc_1:* = this._startPos.subtract(this._endPos);
            var _loc_2:* = (this.currentFrame - this._curOffsetFrame) / (this._fightParams.fightSpeed * 2);
            if (_loc_1.length != 0)
            {
                _loc_1.normalize();
                targetObject3d.setOrientation(_loc_1, null, _loc_2);
            }
            return _loc_2 >= 0.5;
        }// end function

        private function linearMoving() : Boolean
        {
            var _loc_2:Vector3D = null;
            var _loc_5:Vector3D = null;
            var _loc_6:Vector3D = null;
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            var _loc_1:Boolean = false;
            var _loc_3:Boolean = false;
            if (this._endPos.w == -1 && !this._keepGoing)
            {
                _loc_2 = this._templeFight.getPos(this._fightParams.endTargetLabel, this._fightParams.endBoneName, false, true);
                if (_loc_2 == null)
                {
                    return true;
                }
                this._endPos.setTo(_loc_2.x, _loc_2.y, _loc_2.z);
                this._endPos.w = _loc_2.w;
                _loc_3 = true;
            }
            if (this._startPos.w == -1)
            {
                _loc_2 = this._templeFight.getPos(this._fightParams.startTargetLabel, this._fightParams.startBoneName, true, true);
                if (_loc_2 == null)
                {
                    return true;
                }
                if (_loc_2.w == 1 && this._startPos.equals(_loc_2))
                {
                    _loc_2.w = -1;
                }
                this._startPos.setTo(_loc_2.x, _loc_2.y, _loc_2.z);
                this._startPos.w = _loc_2.w;
                _loc_3 = true;
            }
            if (_loc_3)
            {
                _loc_7 = this._endPos.subtract(this._startPos).length;
                this.playDringFrame = _loc_7 / this._fightParams.fightSpeed;
                var _loc_10:int = 1;
                this.playDringFrame = 1;
            }
            var _loc_4:* = this.currentFrame - this._curOffsetFrame;
            if (_loc_4 == 0)
            {
                _loc_5 = this._startPos;
            }
            else if (_loc_4 <= this.playDringFrame)
            {
                if (this.isLinear)
                {
                    _loc_6 = this._endPos.subtract(this._startPos);
                    _loc_8 = _loc_4 * this._fightParams.fightSpeed;
                    _loc_6.normalize();
                    _loc_6.scaleBy(_loc_8);
                    _loc_5 = this._startPos.add(_loc_6);
                }
                else
                {
                    _loc_9 = _loc_4 / this.playDringFrame;
                    FrMathUtil.pointOnCubicBezier2(this._startPos, this._midPos, this._endPos, _loc_9, this._tempVect);
                    _loc_5 = this._tempVect;
                }
            }
            else
            {
                if (!this._keepGoing)
                {
                    _loc_5 = this._endPos;
                }
                else if (this.isLinear)
                {
                    _loc_6 = this._endPos.subtract(this._startPos);
                    _loc_8 = _loc_4 * this._fightParams.fightSpeed;
                    _loc_6.normalize();
                    _loc_6.scaleBy(_loc_8);
                    _loc_5 = this._startPos.add(_loc_6);
                }
                else
                {
                    _loc_9 = _loc_4 / this.playDringFrame;
                    FrMathUtil.pointOnCubicBezier2(this._startPos, this._midPos, this._endPos, _loc_9, this._tempVect);
                    _loc_5 = this._tempVect;
                }
                _loc_1 = true;
            }
            targetObject3d.setPosition(_loc_5.x, _loc_5.y, _loc_5.z, false);
            if (!_loc_1 || this._keepGoing)
            {
                this.setFaceFighted(targetObject3d, this.curPos, _loc_5);
            }
            this.curPos.setTo(_loc_5.x, _loc_5.y, _loc_5.z);
            return _loc_1;
        }// end function

        private function setFaceFighted(param1:Pivot3D, param2:Vector3D, param3:Vector3D) : void
        {
            var _loc_4:* = param2.subtract(param3);
            if (_loc_4.length == 0 || this._preCol3.equals(_loc_4))
            {
                return;
            }
            this._preCol3 = _loc_4;
            _loc_4.normalize();
            var _loc_5:* = _loc_4.crossProduct(Vector3D.Y_AXIS);
            var _loc_6:* = _loc_5.crossProduct(_loc_4);
            var _loc_7:* = param1.transform;
            var _loc_8:int = 0;
            _loc_6.w = 0;
            var _loc_8:* = _loc_8;
            _loc_4.w = _loc_8;
            _loc_5.w = _loc_8;
            _loc_7.copyColumnFrom(0, _loc_5);
            _loc_7.copyColumnFrom(1, _loc_6);
            _loc_7.copyColumnFrom(2, _loc_4);
            param1.setTransform(_loc_7, false);
            return;
        }// end function

        private function updateEndPosHander(event:Event) : void
        {
            var _loc_2:Vector3D = null;
            var _loc_3:Number = NaN;
            if (!this._fightParams || !this._templeFight)
            {
                return;
            }
            if (!this._keepGoing)
            {
                _loc_2 = this._templeFight.getPos(this._fightParams.endTargetLabel, this._fightParams.endBoneName, false, true);
                if (_loc_2)
                {
                    this._endPos.setTo(_loc_2.x, _loc_2.y, _loc_2.z);
                    this._endPos.w = _loc_2.w;
                    _loc_3 = this._endPos.subtract(this._startPos).length;
                    this.playDringFrame = _loc_3 / this._fightParams.fightSpeed;
                    var _loc_4:int = 1;
                    this.playDringFrame = 1;
                }
            }
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose();
            this.clear();
            return;
        }// end function

        public function clear() : void
        {
            this._onhitCallBack = null;
            this._templeFight = null;
            this._fightParams = null;
            if (this._curTargetObj)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._curTargetObj, Engine3dEventName.UPDATE_TRANSFORM_EVENT, this.updateEndPosHander);
                this._curTargetObj = null;
            }
            return;
        }// end function

    }
}
